package database;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "WorstShoppingAppDB";
	private static final int DATABASE_VERSION = 1;
	
	private SQLiteDatabase _database;
	
	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase database) {	
		database.execSQL("CREATE TABLE "+DataBaseKeys.PRODUCT_TABLE+" " 
				+ "(" + DataBaseKeys.PRODUCT_NAME + " TEXT UNIQUE, " 
				+ DataBaseKeys.PRODUCT_ICON + " BLOB, " 
				+ DataBaseKeys.PRODUCT_PRICE + " TEXT, " 
				+ DataBaseKeys.PRODUCT_UNIT + " TEXT)");
		database.execSQL("CREATE TABLE " +
				DataBaseKeys.DATA_TABLE
				+ " ("+DataBaseKeys.LIST_NAME + " TEXT, "
				+ DataBaseKeys.PRODUCT_NAME + " TEXT, "
				+ DataBaseKeys.PRODUCT_COUNT + " INTEGER, "
				+ DataBaseKeys.PRODUCT_MARK + " INTEGER)");
		database.execSQL("CREATE TABLE "+DataBaseKeys.LIST_TABLE+" ("+DataBaseKeys.LIST_NAME+" TEXT UNIQUE)");	
		
		database.execSQL("CREATE TABLE "+DataBaseKeys.TEMP_LIST_TABLE+" " 
				+ "(" + DataBaseKeys.PRODUCT_NAME + " TEXT UNIQUE, "  
				+ DataBaseKeys.PRODUCT_COUNT + " TEXT)");		
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

	}
	
	public void open() throws SQLException
	{
		_database = getWritableDatabase();
	}
	
	public void close() 
	{
		_database.close();
		super.close();
	}

	public SQLiteDatabase getDataBase() {
		return _database;
	}
}
